﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Agr.CQRS.Infrastructure.ServiceBus;
using System.Threading;

namespace Tests.Agr.CQRS.SB
{
    [TestClass]
    public class When_Event_Is_Published_On_ServiceBus
    {
        [TestInitialize]
        public void TestInit()
        {
            //Given
            var sb = new ServiceBus();
            sb.Subscribe<someSortOfEvent>(handleEvent1);
            sb.Subscribe<someSortOfEvent>(handleEvent2);

            //When
            sb.Publish(new someSortOfEvent());
        }

        [TestMethod()]
		public void Then_The_Registered_Eventhandlers_Are_Called()
		{
            Thread.Sleep(100); //because event is fired asynchronously
            Assert.IsTrue(handled2 && handled1);
		}

        private bool handled1;
        private void handleEvent1(someSortOfEvent ev) { handled1 = true; }
        private bool handled2;
        private void handleEvent2(someSortOfEvent ev) { handled2 = true; }

        private class someSortOfEvent { }
    }
}
